module.exports = {
  apps: [
    {
      name: 'stock-melon',
      script: 'src/app.js',
      instances: 'max',
      exec_mode: 'cluster',
      env: {
        NODE_ENV: 'development',
        PORT: 3000
      },
      env_production: {
        NODE_ENV: 'production',
        PORT: 3000
      },
      env_staging: {
        NODE_ENV: 'staging',
        PORT: 3000
      },
      // 日志配置
      log_file: './logs/combined.log',
      out_file: './logs/out.log',
      error_file: './logs/error.log',
      log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
      
      // 进程管理
      min_uptime: '10s',
      max_restarts: 10,
      restart_delay: 4000,
      
      // 监控配置
      pmx: true,
      monitor: true,
      
      // 内存和CPU限制
      max_memory_restart: '1G',
      node_args: '--max-old-space-size=1024',
      
      // 健康检查
      health_check_grace_period: 3000,
      health_check_fatal_exceptions: true,
      
      // 环境变量文件
      env_file: '.env',
      
      // 启动参数
      args: '--color',
      
      // 自动重启
      autorestart: true,
      watch: false,
      ignore_watch: ['node_modules', 'logs', '*.log'],
      
      // 错误处理
      kill_timeout: 5000,
      listen_timeout: 3000,
      
      // 集群配置
      instance_var: 'INSTANCE_ID',
      
      // 日志轮转
      log_type: 'json',
      merge_logs: true,
      
      // 性能监控
      source_map_support: true,
      
      // 优雅关闭
      shutdown_with_message: true,
      wait_ready: true,
      
      // 通知配置
      notify: false,
      
      // 调试配置
      inspect: false,
      inspect_brk: false
    }
  ],
  
  // 部署配置
  deploy: {
    production: {
      user: 'deploy',
      host: 'your-production-host',
      ref: 'origin/main',
      repo: 'git@github.com:your-username/stock-melon.git',
      path: '/var/www/stock-melon',
      'pre-deploy-local': '',
      'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --env production',
      'pre-setup': ''
    },
    staging: {
      user: 'deploy',
      host: 'your-staging-host',
      ref: 'origin/develop',
      repo: 'git@github.com:your-username/stock-melon.git',
      path: '/var/www/stock-melon-staging',
      'pre-deploy-local': '',
      'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --env staging',
      'pre-setup': ''
    }
  }
};
